
#ifndef __GAMESOUND_H__
#define __GAMESOUND_H__

#include <dsound.h>

#define MAX_SOUNDS     256 // max number of sounds in system at once 


// digital sound object state defines
#define SOUND_NULL     0 // " "
#define SOUND_LOADED   1
#define SOUND_PLAYING  2
#define SOUND_STOPPED  3

#define DSVOLUME_TO_DB(volume) ((DWORD)(-30*(100 - volume)))

// this holds a single sound
typedef struct pcm_sound_typ
{
	LPDIRECTSOUNDBUFFER dsbuffer;   // the ds buffer containing the sound
	int state;                      // state of the sound
	int rate;                       // playback rate
	int size;                       // size of sound
	int id;                         // id number of the sound
} pcm_sound, *pcm_sound_ptr;


class CGameSound
{
public:
	CGameSound();
	virtual ~CGameSound();

	int		Init(HWND hWnd);

	int		Load_WAV(char *filename, int control_flags);

	int		Play(int id, int flags, int volume, int rate, int pan);
	int		Replicate_Sound(int source_id);

	LPDIRECTSOUND GetDSound()
	{
		return m_lpds;
	}

	int Set_Volume(int id,int vol)
	{
		// this function sets the volume on a sound 0-100

		if (m_sound_fx[id].dsbuffer->SetVolume(DSVOLUME_TO_DB(vol))!=DS_OK)
			return -1;

		// return success
		return 0;

	} // end Set_Volume

	int Set_Freq(int id,int freq)
	{
		// this function sets the playback rate

		if (m_sound_fx[id].dsbuffer->SetFrequency(freq)!=DS_OK)
			return -1;

		// return success
		return 0;

	} // end Set_Freq

	///////////////////////////////////////////////////////////

	int Set_Pan(int id,int pan)
	{
		// this function sets the pan, -10,000 to 10,0000

		if (m_sound_fx[id].dsbuffer->SetPan(pan)!=DS_OK)
			return -1;

		// return success
		return 0;

	} // end Set_Pan

	int Status(int id)
	{
		// this function returns the status of a sound
		if (m_sound_fx[id].dsbuffer)
		{
			ULONG status; 

			// get the status
			m_sound_fx[id].dsbuffer->GetStatus(&status);

			// return the status
			return(status);

		} // end if
		else // total failure
			return(-1);

	} // end Status

	int Shutdown();
	int Stop_All_Sounds();
	int Stop_Sound(int id);

	int Delete_All_Sounds();
	int Delete_Sound(int id);

protected:
	// directsound stuff
	LPDIRECTSOUND			m_lpds;						// directsound interface pointer
	DSBUFFERDESC			m_dsbd;						// directsound description
	DSCAPS					m_dscaps;					// directsound caps
	HRESULT					m_dsresult;					// general directsound result
	DSBCAPS					m_dsbcaps;					// directsound buffer caps
	LPDIRECTSOUNDBUFFER		m_lpdsbprimary;				// the primary mixing buffer
	pcm_sound				m_sound_fx[MAX_SOUNDS];		// the array of secondary sound buffers

	WAVEFORMATEX			m_pcmwf;					// generic waveformat structure

	HWND					m_hWnd;
};

#endif